import router from "../router";

export default {
    /**
     * 存Tab
     * @param state
     * @param data
     */
    setNavData(state, data) {
        const nav = state.navBarList.find(item => item.path === data.path);
        !nav && state.navBarList.push({...data})
    },
    /**
     * 存视图
     * @param state
     * @param data
     */
    setViewCache(state, data) {
        if (state.viewList.includes(data.componentName)) {
            return;
        }
        state.viewList.push(data.componentName);
    },
    /**
     * 清除Tab
     * @param state
     * @param data
     */
    clearNavItem(state, data) {
        const index = state.navBarList.findIndex(item => item.path === data.path);
        const isActive = router.currentRoute.value.path == state.navBarList[index]["path"];
        const len = state.navBarList.length - 1;
        state.navBarList.splice(index, 1);
        (index == len || isActive) && router.push({path: state.navBarList[state.navBarList.length - 1]["path"]})
    },
    /**
     * 清除所有Tab
     * @param state
     */
    removeAll(state) {
        // state.navBarList = [];
        state.viewList = [];
    },
    /**
     * 切换Tab
     * @param state
     * @param data
     */
    changeTabMutation(state, data) {
        let currentPath = router.currentRoute.value.path;
        let index = 0;
        let len = state.navBarList.length;
        for (let i = 0; i < len; i++) {
            let temp = state.navBarList[i];
            if (currentPath == temp["path"]) {
                index = i + data;
                break;
            }
        }
        index = index < 0 ? 0 : index >= len ? len - 1 : index;
        router.push({path: state.navBarList[index]["path"]})
    },
    /**
     * 清除视图
     * @param state
     * @param viewName
     */
    clearCache(state, data) {
        const index = state.viewList.findIndex(item => item === data);
        state.viewList.splice(index, 1);
    },

    /**
     * 用户登录
     * @param state
     * @param data
     */
    loginMutation(state, data) {
        state.userInfo = data;
    },
    /**
     * 用户登出
     * @param state
     * @param data
     */
    logoutMutation(state, data) {
        state.userInfo = undefined;
        state.permissionMenus = [];
    },
    /**
     * 权限菜单
     * @param state
     * @param data
     */
    permissionMenusMutation(state, data) {
        state.permissionMenus = data;
    }
}